//
//  RadioViewController.m
//  Lianxi2
//
//  Created by lanou on 16/7/3.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *radioListArray;
@property (nonatomic,strong)NSMutableArray *carouselArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RadioViewController

- (NSMutableArray *)carouselArray{
    if (!_carouselArray) {
        _carouselArray  = [NSMutableArray array];
    }
    return _carouselArray;
}
- (NSMutableArray *)radioListArray{
    if (!_radioListArray) {
        _radioListArray = [NSMutableArray array];
    }
    return _radioListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [RequestManager fetchDataWithURL:KRadioUrl parameter:@{} metod:@"GET" compelet:^(NSData *data) {
        //请求数据，分析数据，建立模型，添加数组
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"alllist"]) {
            RadioListModel *a = [[RadioListModel alloc]init];
            [a setValuesForKeysWithDictionary:smallDic];
            [self.radioListArray addObject:a];
            
        }
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {
            NSString *url = smallDic[@"img"];
            [self.carouselArray addObject:url];
            
        }
        
        
       
        
    } updateUI:^{
        
        ScrollView *scroll = [[ScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, (kHeight-64)/3) imageArray:self.carouselArray];
        self.tableView.tableHeaderView = scroll;
        [self.tableView reloadData];
    }];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
       
        [RequestManager fetchDataWithURL:KRadioUpdateUrl parameter:@{@"auth":@"XZU7RH7m1861DC8Z8H8HvkTJxQMGoPLGO9zo4XDA0cWP22NdFSh9d7fo&client=1",@"deviceid":@"6D4DD967-5EB2-40E2-A202-37E64F3BEA31&limit=9&start=9&version=3.0.6"} metod:@"POST" compelet:^(NSData *data) {
           
            NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@",bigDic);
            
        } updateUI:^{
            [self.tableView.mj_header endRefreshing];
        }];
        
    }];
    [self.tableView.mj_header beginRefreshing];
    //上拉加载
    
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioTableViewCell" bundle:nil] forCellReuseIdentifier:@"radio_cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.radioListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    RadioListModel *model = self.radioListArray[indexPath.row];
    RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radio_cell" forIndexPath:indexPath];
    [cell setCellWithModel:model];
   
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioListModel *model = self.radioListArray[indexPath.row];
    RadioListViewController *a = [[RadioListViewController alloc]initWithNibName:@"RadioListViewController" bundle:nil];
    a.mode = model;
    [self.navigationController pushViewController:a animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
